<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确定</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  addBaseContact,
  editBaseContact,
  deleteBaseContact
} from '@/api/potenCus/basePlate/cust1Manage';
import { dictionary } from '@/utils/dictMixin';
import AjaxButton from '@/components/AjaxButton';
import { phone } from '@/utils/validate'

export default {
  name: 'addContact',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    },
    contantypeList: ''
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      conf: { add: '新增',edit: '编辑', delete: '删除' },
    };
  },
  created() {
    this.init();
    console.log(111,this.contantypeList)
  },
  methods: {
    init() {
      console.log('this.rowData联系人',this.rowData)
      this.potentialCustomersId =this.rowData.potentialCustomersId?this.rowData.potentialCustomersId:this.rowData.potentialCustomerId;
      if (this.type === 'edit' || this.type === 'delete') {
        // 更新接口需要的记录 ID
        // this.potentialCustomersId =this.rowData.potentialCustomersId?this.rowData.potentialCustomersId:this.rowData.potentialCustomerId;
        this.contactId = this.rowData.contactId;
        
      }
      if(this.type === 'edit'){
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
      let arr = this.createDictList('1534');
      let newarr = [];
      arr.map(x=>{
        if (x.value !== 15341008) {
          newarr.push(x)
        }
      })
      //this.formList.find(item=>item.fieldName==='contactRelation').itemList = newarr;
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '联系人姓名',
          fieldName: 'contactName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '联系人手机号',
          fieldName: 'contactorPhone',
          placeholder: '请输入',
          maxlength: 11,
          rules: [{ required: true, message: '请输入手机号码', trigger: 'blur' },
                  { trigger: 'blur', validator: phone }],
        },
        {
          type: 'SELECT',
          label: '联系人性别',
          fieldName: 'gender',
          placeholder: '请选择',
          itemList: this.createDictList('1002'),
        },
        {
          type: 'SELECT',
          label: '联系人类型',
          fieldName: 'contactType',
          placeholder: '请选择',
          itemList: this.contantypeList,
          rules: [{ required: true, message: '请选择联系人类型', trigger: 'blur' }],
          change: val =>{
            console.log('val',val)
            if(val == 70341001 ){

            }
          }
        },
        {
          type: 'SELECT',
          label: '关系',
          fieldName: 'contactRelation',
          placeholder: '请选择',
          itemList: this.createDictList('1534'),
          initialValue : '',
        },
        {
          type: 'INPUT',
          label: '联系备注',
          fieldName: 'contactRemark',
          placeholder: '请输入',
        },

      ];
    },
    formChangeHandle(val) {
      // console.log('val', val)
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log('this.saveData', this.saveData,this.type,this.potentialCustomersId)
      if (this.saveData !== null) {
        this.saveData.potentialCustomersId=this.potentialCustomersId
        let res = {}
        if(this.type === 'add'){
          res = await addBaseContact(this.saveData)
        }
        if(this.type === 'edit'){
          this.saveData.contactId = this.contactId
          res = await editBaseContact(this.saveData)
        }
        // if(this.type === 'delete'){
        //   res = await deleteBaseContact(this.contactId)
        // }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      } else {
        this.$message.info('请填写必填项!');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>

<style lang="less" scoped>
</style>
